package com.example.bailin.giftsay.sort.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.bailin.giftsay.R;
import com.example.bailin.giftsay.entity.RaidersDownEntity;
import com.example.bailin.giftsay.sort.LmClickListener;
import com.example.bailin.giftsay.tool.MyApp;
import com.example.bailin.giftsay.tool.NetTool;

/**
 * Created by bailin on 16/8/18.
 * <p/>
 * <p/>
 * 品类/风格/对象 的适配器
 *
 * 已优化
 */
public class PinLeiAdapter extends RecyclerView.Adapter<PinLeiAdapter.PinLeiViewHolder> {
    private RaidersDownEntity entity;
    private int i;
    private int count; // 判断如果count是0  页面显示6个 否则显示全部
    private LmClickListener lmClickListener;

    public void setLmClickListener(LmClickListener lmClickListener) {
        this.lmClickListener = lmClickListener;
    }

    public PinLeiAdapter(int count) {
        this.count = count;
    }

    public void setI(int i) {
        this.i = i;
    }

    public void setEntity(RaidersDownEntity entity) {
        this.entity = entity;
    }

    @Override
    public PinLeiViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(MyApp.getContext()).inflate(R.layout.item_sorypinlei, parent, false);
        PinLeiViewHolder viewHolder = new PinLeiViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(PinLeiViewHolder holder, final int position) {

        NetTool netTool = new NetTool();
        netTool.getImg(entity.getData().getChannel_groups().get(i).getChannels().get(position).getCover_image_url(), holder.iv);
        holder.iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                lmClickListener.onClick
                        (entity.getData().getChannel_groups().get(i).getChannels().get(position).getId()
                                , entity.getData().getChannel_groups().get(i).getChannels().get(position).getName());
            }
        });
    }

    @Override
    public int getItemCount() {
        if (count == 0) {
            return entity.getData().getChannel_groups() == null ? 0 : 6;
        }
        return entity.getData().getChannel_groups() == null ? 0 : entity.getData().getChannel_groups().get(i).getChannels().size();
    }

    public class PinLeiViewHolder extends RecyclerView.ViewHolder {
        public ImageView iv;

        public PinLeiViewHolder(View itemView) {
            super(itemView);
            iv = (ImageView) itemView.findViewById(R.id.iv_item_pinlei);
        }
    }
}
